package org.lanqiao.music.web.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.lanqiao.music.common.dto.AlbumDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@Aspect
public class HotAspect {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Before("execution(* org.lanqiao.music.web.controller.MusicController.getMusicVOById(..)) && args(musicId)")
    public void hotPointCut(Integer musicId){
        System.out.println("apo:"+musicId);
        System.out.println("热点数据service");
        if (redisTemplate.hasKey("hotMusic") == null){
            System.out.println("redis中没有数据");
            redisTemplate.opsForZSet().add("hotMusic",musicId,1);
            redisTemplate.expire("hotMusic",1, TimeUnit.DAYS);
        }
        /*System.out.println(redisTemplate.opsForZSet().incrementScore("hotMusic",musicId,1));*/
        if (redisTemplate.opsForZSet().rank("hotMusic",musicId) == null){
            redisTemplate.opsForZSet().add("hotMusic",musicId,1);
        }else {
            redisTemplate.opsForZSet().incrementScore("hotMusic", musicId, 1);
        }
        if (redisTemplate.opsForZSet().size("hotMusic") == 1){
            redisTemplate.expire("hotMusic",1, TimeUnit.DAYS);
        }

    }

    @Before("execution(* org.lanqiao.music.web.controller.AlbumController.searchAlbumMusic(..)) && args(dto)")
    public void hotAblumCut(AlbumDTO dto){
        System.out.println("apo:"+dto.getAlbumId());
        System.out.println("热点数据service:album");
        if (redisTemplate.hasKey("hotAlbum") == null){
            System.out.println("redis中没有数据");
            redisTemplate.opsForZSet().add("hotAlbum",dto.getAlbumId(),1);
            redisTemplate.expire("hotAlbum",1, TimeUnit.DAYS);
        }
        /*System.out.println(redisTemplate.opsForZSet().incrementScore("hotMusic",musicId,1));*/
        if (redisTemplate.opsForZSet().rank("hotAlbum",dto.getAlbumId()) == null){
            redisTemplate.opsForZSet().add("hotAlbum",dto.getAlbumId(),1);
        }else {
            redisTemplate.opsForZSet().incrementScore("hotAlbum", dto.getAlbumId(), 1);
        }
        if (redisTemplate.opsForZSet().size("hotAlbum") == 1){
            redisTemplate.expire("hotAlbum",1, TimeUnit.DAYS);
        }

    }
}
